package com.dailyblue.java.spring.mvc.controller;

import com.alibaba.fastjson.JSONArray;
import com.dailyblue.java.spring.mvc.bean.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RequestMapping("/four")
@Controller
public class FourthController {

    @GetMapping("/a")
    @ResponseBody  // 标注当前方法的返回结果不是页面跳转，而是返回内容
    public String a() {
        System.out.println("FourthController`a!");
        return "This is FourthController`a!";
    }

    @GetMapping("/b")
    public void b(HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        out.println("This is FourthController`b!");
    }

    @GetMapping(value = "/c",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String c() {
        Users user = new Users();
        user.setAge(23);
        user.setId(1);
        user.setName("张三");
        user.setSex('男');
        return JSONArray.toJSONString(user);
    }

}
